
import { _decorator, Component, instantiate, Node, Prefab, Vec3 } from 'cc';
import EvtManger from '../tools/EvtManger';
import { EventName, prop_config, tasteMainGun_config } from '../manager/AppControll';
import { GameData } from '../system/GameData';
import { DataHelper } from '../system/DataHelper';
import { Loader } from '../tools/LoadManger';
import { TasteGunIcon } from './TasteGunIcon';
import { DropCoin } from './DropCoin';
import { GiftGold } from './GiftGold';
import { SkinDebris } from '../ui/SkinDebris';
import { DropMaterial } from './DropMaterial';
import { DropPearls } from './DropPearls';
const { ccclass, property } = _decorator;


export enum CoinDropType {
    one,
    three,
    ten
}

@ccclass('Drop')
export class Drop extends Component {
    @property(Prefab)
    mPreCoinPrefab: Prefab = null;

    protected onLoad(): void {
        this.RegisteredEvent();
    }

    RegisteredEvent() {
        EvtManger.on(EventName.dropSkin, this, this.SkinDrop);
        EvtManger.on(EventName.dropSkinTriple, this, this.SkinDropTriple);
        EvtManger.on(EventName.dropGiftGold, this, this.GiftDrop);
        EvtManger.on(EventName.dropMaterial, this, this.MaterialDrop);
        EvtManger.on(EventName.dropPearls, this, this.PearlsDrop);
    }

    BossDrop(pos: Vec3) {
        if (!pos) return;
        let obj = DataHelper.GetTasteConfig();
        const length = Object.keys(obj).length;
        const randomNumber: number = Math.floor(Math.random() * length) + 1;
        let id = 90000 + randomNumber;
        let taste: tasteMainGun_config = obj[id];
        let endPos = this.GetEndPoint();
        Loader.LoadPrefab(taste.dropPrefab, (prefab) => {
            let node = instantiate(prefab);
            node.position = pos.clone();
            node.addComponent(TasteGunIcon).InitTasteGunIcon(endPos, this.node.parent.getChildByName("temporaryGun"), taste);
            this.node.addChild(node);
        })
    }

    GiftDrop(position?) {
        let pos = Vec3.ZERO;
        if (position) pos = new Vec3(position.x, position.y - this.node.position.y);
        Loader.LoadUiPrefab("ui/giftGold", (prefab) => {
            let node = instantiate(prefab);
            node.position = pos.clone();
            let endPos = this.GetCoinEndPoint();
            node.addComponent(GiftGold).InitGiftGold(endPos);
            this.node.addChild(node);
        })
    }

    PearlsDrop(num: number, position?) {
        let pos = Vec3.ZERO;
        if (position) pos = new Vec3(position.x, position.y - this.node.position.y);
        Loader.LoadUiPrefab("ui/giftPearls", (prefab) => {
            let node = instantiate(prefab);
            node.position = pos.clone();
            let endPos = this.GetPearlsEndPoint();
            node.addComponent(DropPearls).InitDropPearis(endPos, num);
            this.node.addChild(node);
        })
    }

    MaterialDrop(prop: any, position?) {
        let pos = Vec3.ZERO;
        if (position) pos = new Vec3(position.x, position.y - this.node.position.y);
        Loader.LoadUiPrefab("ui/getGunMaterial", (prefab) => {
            let node = instantiate(prefab);
            node.position = pos.clone();
            let endPos = this.GetMaterialEndPonit();
            node.addComponent(DropMaterial).InitDropMaterial(endPos, prop);
            this.node.addChild(node);
        })
    }

    CoinDrop(pos: Vec3, type: CoinDropType) {
        if (!pos) return;
        let coin = instantiate(this.mPreCoinPrefab);
        this.node.addChild(coin);
        let endPos = this.GetCoinEndPoint();
        coin.addComponent(DropCoin).InitDropCoin(endPos, type);
        coin.position = pos.clone();
    }

    GetEndPoint() {
        let pos = this.node.parent.getChildByName("Ui").getChildByName("Fight").getChildByName("temporaryGun").position.clone();
        pos.x -= this.node.position.x;
        pos.y -= this.node.position.y;
        return pos;
    }

    GetCoinEndPoint() {
        let pos = this.node.parent.getChildByName("Ui").getChildByName("Fight").getChildByName("coinEndPoint").position.clone();
        pos.x -= this.node.position.x;
        pos.y -= this.node.position.y;
        return pos;
    }

    GetSkinEndPoint() {
        let pos = this.node.parent.getChildByName("Ui").getChildByName("Fight").getChildByName("skinEndPoint").position.clone();
        pos.x -= this.node.position.x;
        pos.y -= this.node.position.y;
        return pos;
    }

    GetMaterialEndPonit() {
        let pos = this.node.parent.getChildByName("Ui").getChildByName("Fight").getChildByName("MaterialEndPoint").position.clone();
        pos.x -= this.node.position.x;
        pos.y -= this.node.position.y;
        return pos;
    }

    GetPearlsEndPoint() {
        let pos = this.node.parent.getChildByName("Ui").getChildByName("Fight").getChildByName("PearlsEndPoint").position.clone();
        pos.x -= this.node.position.x;
        pos.y -= this.node.position.y;
        return pos;
    }

    SkinDrop(config, count, position?) {
        let pos = Vec3.ZERO;
        if (position) pos = new Vec3(position.x, position.y - this.node.position.y);
        Loader.LoadUiPrefab("ui/skinDebris", (prefab) => {
            let skinNode = instantiate(prefab);
            skinNode.position = pos.clone();
            let endPos = this.GetSkinEndPoint();
            skinNode.getComponent(SkinDebris).InitSkinDebris(config, count, endPos);
            this.node.addChild(skinNode);
        })
    }

    SkinDropTriple(config, count, position?) {
        let pos = Vec3.ZERO;
        if (position) pos = new Vec3(position.x, position.y - this.node.position.y);
        count *= 3;
        Loader.LoadUiPrefab("ui/skinDebris", (prefab) => {
            let skinNode = instantiate(prefab);
            skinNode.position = pos.clone();
            let endPos = this.GetSkinEndPoint();
            skinNode.getComponent(SkinDebris).InitSkinDebris(config, count, endPos);
            this.node.addChild(skinNode);
        })
    }
}
